package com.cn.service.server.EM.partRegt.Impl;

import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.EM.partRegt.PartSingleRegisterBean;

import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.EM.partRegt.PartSingleRegisterDao;
import com.cn.mapper.dao.db5.EM.partRegt.UserAccount;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.EM.partRegt.PartSingleRegisterService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class PartSingleRegisterServiceImpl implements PartSingleRegisterService {
    private final PartSingleRegisterDao partSingleRegisterDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public PartSingleRegisterServiceImpl(Sys_Sequence_Info_TDAO sysSequenceInfoTdao, PartSingleRegisterDao partSingleRegisterDao) {
        this.partSingleRegisterDao = partSingleRegisterDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;

    }


    @Override
    public List<PartSingleRegisterBean> getEquipmentMappingTable(PartSingleRegisterBean partSingleRegisterBean) {
        return partSingleRegisterDao.getEquipmentMappingTable(partSingleRegisterBean);
    }

    @Override
    public Result insert(PartSingleRegisterBean partSingleRegisterBean) {
        int UniqueCodeCount = partSingleRegisterDao.insertUniqueCodeCheck(partSingleRegisterBean.getPartSingleId());
        if (UniqueCodeCount > 0) {
            return Result.failure("2", "保存失败,单件唯一码重复,请重新输入!!!");
        }
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("PartSingleRegister");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("3", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "PartSingleRegister");
        partSingleRegisterBean.setId(ID);
        //初始外径
        BigDecimal initDiameter = partSingleRegisterBean.getInitDiameter();
        //最小工作面外径
        BigDecimal minWorkingFaceOuterDiameter = partSingleRegisterBean.getMinWorkingFaceOuterDiameter();
        //磨削量
        BigDecimal subtract = initDiameter.subtract(minWorkingFaceOuterDiameter);
        partSingleRegisterBean.setResidualGrindingCapacity(subtract);


        int i = partSingleRegisterDao.insertPartSingleRegister(partSingleRegisterBean);

        int i1 = partSingleRegisterDao.insertPartSingleCurrentInfo(partSingleRegisterBean);

        return Result.success("1", "保存成功!");
    }

    @Override
    public List<PartSingleRegisterBean> getList(int limit, int page, Integer id) {

        return partSingleRegisterDao.getList(limit, page, id);
    }

    @Override
    public Result update(PartSingleRegisterBean partSingleRegisterBean) {
        List<PartSingleRegisterBean> partSingleRegisterBeanList = partSingleRegisterDao.getUniqueCode(partSingleRegisterBean.getId());
        //判断UniqueCode单件唯一码是否改变
        if (!partSingleRegisterBeanList.get(0).getUniqueCode().equals(partSingleRegisterBean.getPartSingleId())) {
            int UniqueCodeCount = partSingleRegisterDao.insertUniqueCodeCheck(partSingleRegisterBean.getPartSingleId());
            if (UniqueCodeCount > 0) {
                return Result.failure("2", "保存失败,单件唯一码重复,请重新输入!!!");
            }

        }
//        List<PartSingleRegisterBean> equipmentMappingTable = partSingleRegisterDao.getEquipmentMappingTable(partSingleRegisterBean);
        partSingleRegisterDao.updatePartSingleRegister(partSingleRegisterBean);
        int i = partSingleRegisterDao.deletePartSingleCurrentInfo(partSingleRegisterBean.getId());
        partSingleRegisterDao.insertPartSingleCurrentInfo(partSingleRegisterBean);
        return Result.success("1", "修改成功");

    }

    @Override
    public Result delete(List<Integer> idList) {
        for (Integer i : idList) {
            partSingleRegisterDao.deletePartSingleRegister(i);
            partSingleRegisterDao.deletePartSingleCurrentInfo(i);
        }

        return Result.success("1", "删除成功");
    }


    @Override
    public Result auditing(List<Integer> idList, int state) {
        ObjectMapper objectMapper = new ObjectMapper();
        UserAccount userAccount = new UserAccount();
        userAccount.setUsername("testUser");
        userAccount.setEmail("test@example.com");
//     {"username":"testUser","email":"test@example.com"}
        String jsonString = "";
        try {
            jsonString = objectMapper.writeValueAsString(userAccount);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        List<PartSingleRegisterBean> partSingleRegisterBeanList = partSingleRegisterDao.callZTQProc("11,9", "", "");
        for (PartSingleRegisterBean partSingleRegisterBean : partSingleRegisterBeanList) {
            String uniqueCode = partSingleRegisterBean.getUniqueCode();
            System.out.println(uniqueCode);
        }
        for (Integer i : idList) {
            int auditing = partSingleRegisterDao.auditing(i, state);

        }
        if (state == 0) {
            return Result.success("1", "弃审成功");
        }
        return Result.success("1", "审核成功");

    }

}
